package com.xg.mina;

import org.apache.commons.lang3.StringUtils;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;

public class ConnectDecoder extends CumulativeProtocolDecoder {

    @Override
    protected boolean doDecode(IoSession ioSession, IoBuffer ioBuffer, ProtocolDecoderOutput protocolDecoderOutput) throws Exception {
        int remaining = ioBuffer.remaining();
        if (remaining > 7) {
            Byte b = ioBuffer.get();
            String s1 = Integer.toBinaryString(b);
            String s = StringUtils.leftPad(s1, 8, '0');
            byte fmt = Byte.valueOf(StringUtils.substring(s, 0, 2));
            System.out.println("fmt类型："+fmt);
            Byte byteStreamId = Byte.valueOf(StringUtils.substring(s, 2));
            switch (byteStreamId) {
                case  0:
                    //todo
                    break;
                case 1:
                    //todo
                    break;
                default:
                   //streamId只有一位

            }

        }
        protocolDecoderOutput.write(ioBuffer);
        return true;
    }
}
